餵參數
、取回資料
、判斷資料是否如預期
、拿回資料處理後當成呼叫下一回合 API 的參數
import asana
import configparser
from asana.rest import ApiException
class Kanban():
def __init__(self) -> None:
# Configure OAuth2 access token for authorization: oauth2
configuration = asana.Configuration()
config = configparser.ConfigParser()
config.read('config.ini')
configuration.access_token = config['Asana']['asana_token']
# create an instance of the API class
self.client = asana.ApiClient(configuration)
self.workspaces = None
self.projects = None
self.tasks = None
self.get_all()
def get_workspaces(self):
try:
# create an instance of the API class
api_instance = asana.WorkspacesApi(self.client)
# Get multiple workspaces
api_response = api_instance.get_workspaces()
data = api_response.to_dict()['data']
self.workspaces = [ d['gid'] for d in data ]
except ApiException as e:
print("Exception when calling WorkspacesApi->get_workspaces: %s\n" % e)
def get_all(self):
self.get_workspaces()
# self.get_projects()
# self.get_tasks()
import asana
import configparser
config.ini
讀取from asana.rest import ApiException
Exception
就是在做例外處理的Kanban
建構子Kanban
類別內的方法 get_workspaces()
get_workspaces()
為例,因為後續要陸續取回很多資料,但是外送員只要請一個就好了,差別就在於叫他去什麼店買什麼東西而已to_dict()
data
,所以弄成 data = api_response.to_dict()['data']
self.workspaces = [ d['gid'] for d in data ]
,這邊用到 list generator 蒐集迭代資料內特定索引的值,看不懂沒關係,看久了就習慣了